package com.roy.ESClient;

import java.io.IOException;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class ESClientMain2 {

	private static HttpHost[] esHosts = new HttpHost[] { new HttpHost("172.16.48.53", 9200) };
	private static long totalHits;

	public static void main(String[] args) {
		 // 1.创建RestClient对象
		RestClientBuilder restClient = RestClient.builder(esHosts);
		RestHighLevelClient client = new RestHighLevelClient(restClient);
        // 2.创建BoolQueryBuilder对象
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        // 3.设置boolQueryBuilder条件
        MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders
                .matchPhraseQuery("log_level", "INFO");
        MatchPhraseQueryBuilder matchPhraseQueryBuilder2 = QueryBuilders
                .matchPhraseQuery("fields.log_app", "ssmg");
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders
                .rangeQuery("@timestamp")
                .from("2020-04-13T09:42:58.443Z");
        // 子boolQueryBuilder条件条件，用来表示查询条件or的关系
        BoolQueryBuilder childBoolQueryBuilder = new BoolQueryBuilder()
                .should(matchPhraseQueryBuilder)
                .should(matchPhraseQueryBuilder2);
        // 4.添加查询条件到boolQueryBuilder中
        boolQueryBuilder
                .must(childBoolQueryBuilder)
                .must(rangeQueryBuilder);
		
     // 1.创建并设置SearchSourceBuilder对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 查询条件--->生成DSL查询语句
        searchSourceBuilder.query(boolQueryBuilder);
        // 第几页
        searchSourceBuilder.from(0);
        // 每页多少条数据
        searchSourceBuilder.size(100);
        // 获取的字段（列）和不需要获取的列
//        searchSourceBuilder.fetchSource(new String[]{"log_txCode", "source"}, new String[]{});
        // 设置排序规则
//        searchSourceBuilder.sort("postdate", SortOrder.ASC);
        // 设置超时时间为2s
        searchSourceBuilder.timeout(new TimeValue(2000));
        
//        searchSourceBuilder.aggregation(AggregationBuilders.dateHistogram(name))
 
        // 2.创建并设置SearchRequest对象
        SearchRequest searchRequest = new SearchRequest();
        // 设置request要搜索的索引和类型
        searchRequest.indices("log-ics-consumer-*").types("doc");
        // 设置SearchSourceBuilder查询属性
        searchRequest.source(searchSourceBuilder);
        
 
        // 3.查询
        try {
			SearchResponse searchResponse = client.search(searchRequest);
			totalHits = searchResponse.getHits().totalHits;
			SearchHits hits = searchResponse.getHits();
			for(SearchHit hit:hits) {
				System.out.println(hit.getSourceAsString());
			}
			System.out.println(totalHits);
			client.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
